#!/usr/bin/env bash
# set -x
set -euo pipefail

cd $(dirname ${BASH_SOURCE[0]})
SHELL_FOLDER=$(pwd)
DEPLOY_ROOT=$(cd ../ && pwd)
source $DEPLOY_ROOT/init/base.sh

mkdir -pv $DEPLOY_ROOT/packages

if [ $(uname -m) == "x86_64" ]; then
  python_pkg_name="python-3.11.11-manylinux_2_28-x86_64.tar.gz"
elif [ $(uname -m) == "aarch64" ]; then
  python_pkg_name="python-3.11.8-linux-aarch64.tar.gz"
fi

python_dir=${python_pkg_name//.tar.gz/} && echo $python_dir

if [ ! -f $DEPLOY_ROOT/packages/${python_pkg_name} ]; then
  cd $DEPLOY_ROOT/packages
  wget ${DOWNLOAD_URL}/${python_pkg_name} --no-check-certificate
fi

mkdir -pv $HOME/pyenv
cd $HOME/pyenv
if [ ! -d "python3" ] && [ ! -L "python3" ];then
  tar -zxf $DEPLOY_ROOT/packages/${python_pkg_name} -C $HOME/pyenv/
  cd $HOME/pyenv/
  ln -sf $python_dir python3
  ln -sf $python_dir/ python3.11
fi

mkdir -pv ~/.local/bin
set +e
cat ~/.bashrc | grep  'pyenv'
if [ $? -ne 0 ]; then
  echo "# Env of Python3" >> ~/.bashrc
  echo 'export PATH=$HOME/pyenv/python3/usr/bin:$PATH' >> ~/.bashrc
  echo 'export PATH=$HOME/.local/bin:$PATH' >> ~/.bashrc
fi
set -e
